<?php

namespace App\Controller\Api;

use App\Base\BaseController4Restful;
use App\Entity\MoneyConfig;
use App\Repository\MoneyConfigRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiMoneyConfigController extends BaseController4Restful
{
    /**
     * @Route("/api/moneyConfigs", name="apiGetMoneyConfigs",methods={"GET","POST"})
     */
    public function apiGetMoneyConfigs(MoneyConfigRepository $repository,Request $request)
    {
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
                /**@var $MoneyConfig MoneyConfig*/
                $MoneyConfig = $this->createEntity($data,MoneyConfig::class);
                $this->myEm()->persist($MoneyConfig);$this->myEm()->flush();
                return $this->json_out(200,"创建成功", $MoneyConfig);
            default:
                if (!$page = $this->getParamRequired("page")) {$page = 1;}
                $limit = 10;
                $offset = (int)abs($limit * ($page - 1));
                $total = $repository->count([]);
                $this->setMeta($page,$total,$limit);
                $list = $repository->findBy(array(),array(),$limit,$offset);
                return $this->json_out(200,"获取成功", $list);
        }
    }

    /**
     * @Route("/api/moneyConfigs/{id}", name="apiAddMoneyConfigs",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateMoneyConfigs(MoneyConfig $MoneyConfig,Request $request)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($MoneyConfig);$this->myEm()->flush();
                return $this->json_out(200,"已移除");
            case "PATCH":
            case "PUT":
                /**@var $MoneyConfig MoneyConfig*/
                $MoneyConfig = $this->updateEntity($data,MoneyConfig::class,$MoneyConfig);
                break;
            case "Get":
            default:
        }
        $this->myEm()->persist($MoneyConfig);$this->myEm()->flush();
        return $this->json_out(200,"获取成功", $MoneyConfig);
    }
}
